package com.ximu.message;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class DB {
	
	static {
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}catch(ClassNotFoundException e){
			System.out.println(e);
		}
	}
	
	private String sqlUrl = "jdbc:mysql://localhost:3306/myproject";
	
	private String user = "root";
	
	private String password = "";
	
	Connection conn = null;
	
	Statement stmt = null;
	
	ResultSet rs = null;
	
	private Connection getConnection() throws SQLException{
		return DriverManager.getConnection(sqlUrl, user, password);
	}
	
	public ResultSet query(String sql) throws SQLException{
		try{
			conn = getConnection();
			stmt = (Statement) conn.createStatement();
			rs = (ResultSet) stmt.executeQuery(sql);
		}finally{
		}
		return rs;
	}
	
	public int update(String sql) throws SQLException{
		conn = getConnection();
		stmt = (Statement) conn.createStatement();
		return stmt.executeUpdate(sql);
	}
	
	public void free(){
		try{
			rs.close();
			stmt.close();
			conn.close();
		}catch(SQLException e){
			System.out.println(e.getMessage());
		}
	}
}
